#pragma once

#include <QKeyEvent>
#include <gameboycore/joypad.h>
#include <map>

class InputMapper
{
public:

	enum class State
	{
		PRESSED, RELEASED
	};

	InputMapper(gb::Joy::Ptr& joy) : m_joy(joy)
	{
		// TODO: read key map from file
		keymap.insert({ Qt::Key_W, gb::Joy::Key::UP });
		keymap.insert({ Qt::Key_A, gb::Joy::Key::LEFT });
		keymap.insert({ Qt::Key_D, gb::Joy::Key::RIGHT });
		keymap.insert({ Qt::Key_S, gb::Joy::Key::DOWN });
		keymap.insert({ Qt::Key_J, gb::Joy::Key::A });
		keymap.insert({ Qt::Key_K, gb::Joy::Key::B });
		keymap.insert({ Qt::Key_Return, gb::Joy::Key::START });
		keymap.insert({ Qt::Key_Shift, gb::Joy::Key::SELECT });
	}

	void process(State state, int keycode)
	{
		if (!m_joy) return;

		auto gbkey = keymap[keycode];

		if (state == State::PRESSED)
			m_joy->press(gbkey);
		else
			m_joy->release(gbkey);
	}

	~InputMapper() = default;

private:
	gb::Joy::Ptr& m_joy;
	std::map<int, gb::Joy::Key> keymap;
};